﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using TPL.Xna.Common;

namespace GoldDigger.Sprites
{
    public class SpriteSheetItemInfo
    {
        public Texture2D Image { get; set; }
        public Point SheetSize { get; set; }
        public Rectangle FrameSize { get; set; }
        public bool RandomOrder { get; set; }
    }

    internal class SpriteSheetItemSprite : ItemSprite
    {
        private readonly Vector2 _frameSize;

        public SpriteSheetItemSprite(SpriteSheetItemInfo spriteSheetItemInfo)
            : base(spriteSheetItemInfo.Image)
        {
            _frameSize = new Vector2(spriteSheetItemInfo.FrameSize.Width, spriteSheetItemInfo.FrameSize.Height);
            // Set origin at the center of the image
            Origin = _frameSize / 2;

            AddAnimator(new SpriteSheetAnimator(spriteSheetItemInfo.SheetSize, spriteSheetItemInfo.FrameSize, spriteSheetItemInfo.RandomOrder));
        }

        /// <summary>
        /// Add area as rectangle around the image.
        /// </summary>
        private SpriteArea _area;
        public override SpriteArea SpriteArea
        {
            get
            {
                if (_area != null) return _area;

                var normal = _frameSize * Scale;
                var scaled = normal * SpriteAreaScale;
                var pos = normal - scaled;

                _area = new RectangleSpriteArea(Screen.ScreenManager.GraphicsDevice, pos, scaled);
                _area.Shape.Origin = normal / 2;
                return _area;
            }
        }
    }
}